﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace WebAppDemo
{
    /// <summary>
    /// Test 的摘要说明
    /// </summary>
    public class Test : IHttpHandler
    {

        //public void ProcessRequest(HttpContext context)
        //{
        //    context.Response.ContentType = "text/plain";
        //    context.Response.Write("Hello World");
        //    context.Response.Redirect("http://www.163.com");
        //}

        public void ProcessRequest(HttpContext context)
        {

            //Capture File From Post
            
            HttpPostedFile file = context.Request.Files["fileToUpload"];
            if (file == null)
            {    string error = "please select file";

            context.Response.ContentType = "text/plain";
            context.Response.Write(error);
            return;

            }
        


            //Optional: Convert to File to binary if you need to forward it to a video encoding service like Panda Stream
            //BinaryReader b = new BinaryReader(file.InputStream);
            //byte[] binData = b.ReadBytes(file.ContentLength);
           // b.Close();

            
            //Or just save it locally
            string path = context.Server.MapPath("~//download//");//+ DateTime.Now.ToString("yyyyMMddHHmmss"); 
          
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
           // string savename = "123"; //System.DateTime.Now.ToLongTimeString();
           // path = path + savename+".jpg";
            file.SaveAs(path+file.FileName);
            //file.SaveAs()


            string result = "File Saved Successfully";

            context.Response.ContentType = "text/plain";
            context.Response.Write(result);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}